//
//  FilterCore.m
//  117goFramework
//
//  Created by Yunhai.Wu on 15/7/30.
//  Copyright (c) 2015年 117go. All rights reserved.
//

#import "QWHttpFilterCore.h"
#import "IQWHttpFilter.h"
#import "QWHttpEngineConfig.h"
#import "QWLoggingMacros.h"

static QWHttpFilterCore *sharedObject;

@interface QWHttpFilterCore ()
@property (nonatomic, strong) NSMutableArray *filters;
@end

@implementation QWHttpFilterCore

-(void)filter:(id<IQWHttpResponse>)res {
    for (id<IQWHttpFilter> filter in _filters) {
        if (![filter doFilter:res]) {
            break;
        }
    }
}

-(void) singleInit {
    self.filters = [[NSMutableArray alloc] init];
    
    NSArray *fs = [[QWHttpEngineConfig sharedInstance] filters];
    for (Class f in fs) {
        [self.filters addObject:[[f alloc] init]];
    }
}

+(instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedObject = [[QWHttpFilterCore alloc] init];
        [sharedObject singleInit];
    });
    return sharedObject;
}

-(id)mutableCopy {
    return self;
}

-(id)copy {
    return self;
}

+(id)allocWithZone:(NSZone *)zone {
    @synchronized (self) {
        if (sharedObject == nil) {
            sharedObject = [super allocWithZone:zone];
        }
    }
    return sharedObject;
}

@end
